package com.projects.xenia.documents_activities;

import com.projects.xenia.R;
import com.projects.xenia.global_modules.MainGlobalModule;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.FilterQueryProvider;
import android.widget.ImageView;
import android.widget.SimpleCursorAdapter;

public class Activity_TaskAdd extends Activity {

	private Context Ctx;
	private SimpleCursorAdapter Source_Adapter;
	private Cursor Source_Cursor;
	private AutoCompleteTextView _task_text;
	
	private ImageView _add;
	private ImageView _backward;
	
	private void GetSource_Cursor (String SubStringParameter) {
		
		SubStringParameter = "%"+SubStringParameter+"%";
		Source_Cursor = MainGlobalModule.DBE.query("xref_TaskTemplates", new String []{"id as _id ","descr"}, "descr like ? ", new String []{SubStringParameter}, null, null, "descr asc");		
				
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {	
		super.onCreate(savedInstanceState);
		
		Ctx = this;
		
        requestWindowFeature(Window.FEATURE_NO_TITLE);            
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.task_add);		

		_task_text = (AutoCompleteTextView)findViewById(R.id._task_text);
		_task_text.requestFocus();
		
		GetSource_Cursor ("");
		
		Source_Adapter = new SimpleCursorAdapter (Ctx,android.R.layout.simple_dropdown_item_1line,Source_Cursor,new String[] {"descr"},new int[] {android.R.id.text1});
				
		Source_Adapter.setCursorToStringConverter(new SimpleCursorAdapter.CursorToStringConverter() 
        {
            public CharSequence convertToString(Cursor cursor) 
            {	 
                return cursor.getString(1);
            }
            
        });		
		
		
		Source_Adapter.setFilterQueryProvider(new FilterQueryProvider() {			
			@Override
			public Cursor runQuery(CharSequence constraint) {
				if (constraint==null){
					constraint = " ";
				} 
				GetSource_Cursor(constraint.toString());
				return Source_Cursor;
			}
		});
		
		_task_text.setAdapter(Source_Adapter);
		_task_text.setThreshold(0);		
		_task_text.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				((AutoCompleteTextView)v).showDropDown();
			}
		});		
		
		_task_text.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
				Source_Cursor.moveToPosition(arg2);								
			}
		});
		
		
		_add = (ImageView)findViewById(R.id._add);
		_add.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Finish (Activity.RESULT_OK);
			}
		});		
		
		_backward = (ImageView)findViewById(R.id._backward);
		_backward.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				Finish (Activity.RESULT_CANCELED);
			}
		});
		
	
		
	}
	
	private void Finish (int Result) {
			
		Intent DataIntent = new Intent();
		if (Source_Cursor!=null && Source_Cursor.getPosition()>-1) {
			DataIntent.putExtra("_id", Source_Cursor.getString(0));
			DataIntent.putExtra("_descr", Source_Cursor.getString(1));
		}
			setResult(Result,DataIntent);
			finish ();	
		
	}
}
